package com.lanxin.oa.approve.dao;

import com.lanxin.base.BaseDaoImpl;
import com.lanxin.bean.LcFlow;
import com.lanxin.bean.LcNode;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class NodeDaoImpl extends BaseDaoImpl implements NodeDao {

    @Override
    public List<LcNode> selectById(Integer flowid) {

        return queryAll("from LcNode l left join fetch l.dept  left join fetch l.job where l.parentid="+flowid);
    }

    @Override
    public LcFlow selectLcFlowId(Integer flowid) {
        return (LcFlow) selectByHQL("from LcFlow l  where l.oid="+flowid);
    }

    @Override
    public Number selectNodeById(Integer oid) {
        return (Number) selectByHQL("select count(a.oid) from LcApproval a where a.nodeid="+oid);
    }

    @Override
    public LcNode getNextNode(Integer oid) {

        return (LcNode) selectByHQL("from LcNode n where n.prenodeid="+oid);
    }

    @Override
    public LcNode getNodeInfo(Integer oid) {

        return (LcNode) selectByHQL("from LcNode n where n.oid="+oid);
    }

    @Override
    public LcNode getFirstNode(Integer parentid) {

        return (LcNode) selectByHQL("from LcNode n where n.parentid=" + parentid + " and n.prenodeid is null");
    }

    @Override
    public LcNode selectByName(String name, Integer oid) {
        String hql = "from LcNode n where n.name='" + name + "' ";
        LcNode lcNode = null;
        if (oid != null) {
            hql = hql + " and n.oid=" + oid;
            lcNode = (LcNode) selectByHQL(hql);
            if (lcNode != null) {
                return null;
            }
            lcNode = (LcNode) selectByHQL("from LcNode n where n.name='" + name + "' ");
            if (lcNode == null) {
                return null;
            }
        } else {
            return (LcNode) selectByHQL(hql);
        }

        return lcNode;
    }
}
